package com.example.ui.share;

import java.util.ArrayList;
import java.util.List;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.service.MainService;

import com.example.util.Util;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.TextView;

@SuppressLint("HandlerLeak")
public class CreateCircleGroupActivity extends FragmentActivity {

	private Button titleLeftBtn, titleRightBtn;
	private TextView titleText;

	String circleName = "";
	String circleIntroduce = "";
	public static String circleIndustry = "";
	public static int circleIndustryid = -1;
	String province = "";
	String city = "";
	String provinceID;
	String cityID;

	private List<String> list;
	String Filename = "";
	int isOpen;
	int isJoin;
	private String data;
	private static String result;
	private CreateCircleGroup cc;

	public static String path = "";

	private Intent myIntent;
	public String ccgStr, ccgHeadImg, ccgName, ccgIntroduce;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_cicle);
		myIntent = getIntent();
		ccgStr = myIntent.getStringExtra("create_update");
		if (!"create".equals(ccgStr)) {
			ccgHeadImg = myIntent.getStringExtra("headImg");
			ccgName = myIntent.getStringExtra("titleName");
			ccgIntroduce = myIntent.getStringExtra("intro");
		}

		findView();
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		cc = new CreateCircleGroup();
		ft.add(R.id.cc_rl, cc);
		ft.commit();

	}

	private void findView() {
		titleLeftBtn = (Button) findViewById(R.id.layout_title_left_btn);
		titleRightBtn = (Button) findViewById(R.id.layout_title_right_btn);
		titleText = (TextView) findViewById(R.id.layout_title_middle_text);
		titleLeftBtn.setText("返回");
		if ("create".equals(ccgStr)) {
			titleText.setText("创建圈子");
			titleRightBtn.setText("提交");
		} else if ("update".equals(ccgStr)) {
			titleText.setText("圈子管理");
			titleRightBtn.setText("完成");
		} else {
			titleText.setText("圈子");
			titleRightBtn.setVisibility(View.INVISIBLE);
		}

		titleRightBtn.setOnClickListener(ccg_ocl);
		titleLeftBtn.setOnClickListener(ccg_ocl);

	}

	// 创建圈子
	Runnable ccgsumbit_run = new Runnable() {

		@Override
		public void run() {

			data = NetRequest.postRequestCreateCircle(list);
			result = AnalyzingJson.return_iaresult(data);
			h.sendEmptyMessage(2);
		}
	};
	// 更新圈子
	Runnable ccgUpdate_run = new Runnable() {

		@Override
		public void run() {

			data = NetRequest.postRequestUpdateCircle(list);
			result = AnalyzingJson.return_iaresult(data);
			h.sendEmptyMessage(3);
		}
	};

	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

			case 2:
				if (result.equals("true")) {
					Toast.makeText(CreateCircleGroupActivity.this,
							"创建成功,请等待审核...", Toast.LENGTH_LONG).show();
					finish();
				} else {
					Toast.makeText(CreateCircleGroupActivity.this, "创建失败，请重试",
							Toast.LENGTH_LONG).show();
				}
				break;
			case 3:
				if (result.equals("true")) {
					Toast.makeText(CreateCircleGroupActivity.this, "更新成功",
							Toast.LENGTH_LONG).show();
					finish();
				} else {
					Toast.makeText(CreateCircleGroupActivity.this,
							"网络连接失败，请重试", Toast.LENGTH_LONG).show();
				}
				break;
			default:
				break;
			}

		};

	};

	private OnClickListener ccg_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.layout_title_left_btn:
				finish();
				break;

			case R.id.layout_title_right_btn:
				if ("create".equals(ccgStr)) {
					getCC_Data("create");// 创建圈子
				} else {
					getCC_Data("update");// 更新圈子
				}

				break;

			default:
				break;
			}
		}
	};

	private void getCC_Data(String isCreate) {
		cc.getdata();
		if (!"".equals(path) || path != null) {
			if (!"".equals(circleName) || !"".equals(circleIntroduce)
					|| !"".equals(circleIndustry) || !"".equals(province)) {
				list = new ArrayList<String>();
				list.add(HSApplication.sp.getString("userid",""));
				list.add(circleName);
				list.add(circleIntroduce);
				list.add(circleIndustryid + "");
				list.add(provinceID);
				list.add(cityID);
				list.add(isOpen + "");
				list.add(isJoin + "");
				list.add(path);
				if ("create".equals(isCreate)) {
					new Thread(ccgsumbit_run).start();
				} else {
					new Thread(ccgUpdate_run).start();
				}

			} else {
				Util.toast(CreateCircleGroupActivity.this, "请填写完整信息");
			}
		} else {
			Util.toast(CreateCircleGroupActivity.this, "请先上传圈子图片");
		}
	}
}
